package bg.uni_sofia.conf_manager.beans;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import bg.uni_sofia.conf_manager.model.Conference;
import bg.uni_sofia.conf_manager.model.Lecture;
import bg.uni_sofia.conf_manager.model.Lecturer;
import bg.uni_sofia.conf_manager.model.TransferConference;

@Stateless
@Path("/lecture")
public class LectureBean {
	@PersistenceContext
	private EntityManager em;
	@Inject LoginBean sessionData;
	
	public LectureBean() {
		super();
	}
    
    @POST
    @Consumes("application/json")
    public Lecture addLecture(TransferConference newTrans){
    	Lecture existingLecture = getLectureByTitle(newTrans.getTitle());
    	if(existingLecture != null){
    		return null;
    	}
    	
    	TypedQuery<Lecturer> queryLecturer = em.createNamedQuery(
				"getLecturerByName", Lecturer.class);
    	queryLecturer.setParameter("username", sessionData.getUsername());
    	Lecturer lecturerFromDB = queryLecturer.getResultList().get(0);
    	Date date = new Date();
    	
    	TypedQuery<Conference> query = em.createNamedQuery(
				"getConferenceByName", Conference.class);
		query.setParameter("name", newTrans.getConfName());
		Conference conf = query.getResultList().get(0);
    	Lecture newLecture = new Lecture(newTrans.getTitle(), newTrans.getDescription(), conf, newTrans.getLength(), date, newTrans.getDate(), newTrans.getTime(), date, 0, lecturerFromDB);
    	conf.addLecture(newLecture);
    	em.persist(newLecture);
    	em.merge(conf);
    	return newLecture;
    }

	@GET
	@Path("/{id}")
	@Produces("application/json")
	public Lecture getLecturesByLecturer(@PathParam("id") long id) {
		TypedQuery<Lecture> query = em.createNamedQuery(
				"getLecturesByLecturer", Lecture.class);
		query.setParameter("id", id);
		return query.getResultList().get(0);
	}
	@GET
	@Path("/{id}")
	@Produces("application/json")
	public List<Lecture> getLecturesByConference(@PathParam("id") long id) {
		TypedQuery<Lecture> query = em.createNamedQuery(
				"getLecturesByConference", Lecture.class);
		query.setParameter("id", id);
		return query.getResultList();
	}

	@GET
	@Path("/{status}")
	@Produces("application/json")
	public List<Lecture> getLecturesByStatus(int status) {
		TypedQuery<Lecture> query = em.createNamedQuery("getLecturesByStatus",
				Lecture.class);
		query.setParameter("status", status);
		return query.getResultList();
	}

	@POST
	@Path("/getLectureByTitle")
	@Consumes("application/json")
	public Lecture getLectureByTitle(String title) {
		TypedQuery<Lecture> query = em.createNamedQuery("getLectureByTitle",
				Lecture.class);
		query.setParameter("title", title);
		if (query.getResultList().size() > 0) {
			Lecture lec = query.getResultList().get(0);
			return lec;
		} else {
			return null;
		}
		
	}
	
	@POST
	@Path("/getConferenceByLectureByTitle")
	@Consumes("application/json")
	public Conference getConferenceByLectureByTitle(String title) {
		TypedQuery<Lecture> query = em.createNamedQuery("getLectureByTitle",
				Lecture.class);
		query.setParameter("title", title);
		if (query.getResultList().size() > 0) {
			Lecture lec = query.getResultList().get(0);
			return lec.getConference();
		} else {
			return null;
		}
		
	}
	
	@POST
	@Path("/approveLectureByTitle")
	@Consumes("application/json")
	public void approveLecture(String title) {
		TypedQuery<Lecture> query = em.createNamedQuery("getLectureByTitle",
				Lecture.class);
		query.setParameter("title", title);
		Lecture lecture = query.getResultList().get(0);
		if(lecture.getStatus() != 1){
			lecture.setStatus(1);
			lecture.setLastModified(new Date());
			em.merge(lecture);
		}
		
	}
	
	
	@POST
	@Path("/disapproveLectureByTitle")
	@Consumes("application/json")
	public void disapproveLecture(String title) {
		TypedQuery<Lecture> query = em.createNamedQuery("getLectureByTitle",
				Lecture.class);
		query.setParameter("title", title);
		Lecture lecture = query.getResultList().get(0);
		if(lecture.getStatus() != -1){
			lecture.setStatus(-1);
			lecture.setLastModified(new Date());
			em.merge(lecture);
		}
	}

}
